package com.ruoyi.system.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.system.domain.vo.DutyUserCountVO;
import com.ruoyi.system.entity.PreventionDevice;
import com.ruoyi.system.entity.PreventionHiddenRegister;
import com.ruoyi.system.entity.SysDutyUser;
import com.ruoyi.system.mapper.PreventionDeviceMapper;
import com.ruoyi.system.mapper.PreventionHiddenRegisterMapper;
import com.ruoyi.system.mapper.SysDutyUserMapper;
import com.ruoyi.system.service.SysDutyUserService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 绑定履职负责人(SysDutyUser)表服务实现类
 *
 * @author makejava
 * @since 2022-11-15 14:09:01
 */
@Service("sysDutyUserService")
public class SysDutyUserServiceImpl implements SysDutyUserService {
    @Resource
    private SysDutyUserMapper sysDutyUserDao;

    @Resource
    private PreventionDeviceMapper preventionDeviceMapper;

    @Resource
    private PreventionHiddenRegisterMapper preventionHiddenRegisterMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysDutyUser queryById(Integer id) {
        return this.sysDutyUserDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysDutyUser 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SysDutyUser> queryByPage(SysDutyUser sysDutyUser) {
        List<SysDutyUser> sysDutyUsers = sysDutyUserDao.queryAllByLimit(sysDutyUser);
        return sysDutyUsers;
    }

    /**
     * 新增数据
     *
     * @param sysDutyUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysDutyUser insert(SysDutyUser sysDutyUser) {
        this.sysDutyUserDao.insert(sysDutyUser);
        return sysDutyUser;
    }

    /**
     * 修改数据
     *
     * @param sysDutyUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysDutyUser update(SysDutyUser sysDutyUser) {
        this.sysDutyUserDao.update(sysDutyUser);
        return this.queryById(sysDutyUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sysDutyUserDao.deleteById(id) > 0;
    }

    @Override
    public SysDutyUser getByDutyType(String dutyType) {
        return sysDutyUserDao.getByDutyType(dutyType);
    }

    @Override
    public SysDutyUser getByUserId(Long userId) {
        return sysDutyUserDao.getByUserId(userId);
    }

    @Override
    public List<DutyUserCountVO> toCountSysDuty(String startTime, String endTime) {
        List<DutyUserCountVO> dutyUserCountVOS = new ArrayList<>();
        List<PreventionDevice> preventionDevices = preventionDeviceMapper.queryAllByLimit(new PreventionDevice());
        // 汇总-总排查任务
        Double HZCount = 0.00;
        // 汇总-总排查任务 完成任务数
        Double HZWCount = 0.00;
        // 汇总-主要负责人总排查任务
        Double HZZCount = 0.00;
        // 汇总-主要负责人 完成任务数
        Double HZZWCount = 0.00;
        // 汇总-技术负责人总排查任务
        Double HZJCount = 0.00;
        // 汇总-技术负责人 完成任务数
        Double HZJWCount = 0.00;
        // 汇总-操作负责人总排查任务
        Double HZCCount = 0.00;
        // 汇总-操作负责人 完成任务数
        Double HZCWCount = 0.00;
        // 汇总-总发现隐患数量
        Integer HZHiddenCount = 0;
        // 汇总-总完成整改数量
        Integer HZWHiddenCount = 0;
        DutyUserCountVO dutyUserCount = new DutyUserCountVO();
        for (PreventionDevice preventionDevice : preventionDevices) {
            if (preventionDevice.getDangerLevel().equals("无重大")) {
                continue;
            }
            DutyUserCountVO dutyUserCountVO = new DutyUserCountVO();
            String dangerName = preventionDevice.getDangerName();
            // 1-主要负责人 2-技术负责人 3-操作负责人
            // 统计隐患数量
            Integer hiddenCount = 0;
            // 保留隐患id
            List<Integer> hiddenIds = new ArrayList<>();
            // 获取主要负责人在某个时间段的任务
            List<Map<String,String>> Zlist = preventionDeviceMapper.getDutyCount(startTime,endTime,dangerName,"1");
            // 任务完成数
            Double ZCount = 0.00;
            for (Map<String, String> map : Zlist) {
                String state = map.get("taskState");
                if (state.equals("已排查")) {
                    ZCount ++;
                }
                if (map.containsKey("hiddenId")) {
                    String hiddenId = String.valueOf(map.get("hiddenId"));
                    if (StrUtil.isNotEmpty(hiddenId)) {
                        hiddenCount ++;
                        hiddenIds.add(Integer.valueOf(hiddenId));
                    }
                }
            }
            HZZCount += Zlist.size();
            HZZWCount += ZCount;
            // 获取完成率
            String Zrate = "0.00%";
            if (Zlist.size() != 0) {
                Double b = ZCount / Zlist.size() * 100;
                Zrate = NumberUtil.round(b,2) + "%";
            } else {
                Zrate = "100.00%";
            }
            // 获取技术负责人在某个时间段的任务
            List<Map<String,String>> Jlist = preventionDeviceMapper.getDutyCount(startTime,endTime,dangerName,"2");
            // 任务完成数
            Double JCount = 0.00;
            for (Map<String, String> map : Jlist) {
                String state = map.get("taskState");
                if (state.equals("已排查")) {
                    JCount ++;
                }
                if (map.containsKey("hiddenId")) {
                    String hiddenId = String.valueOf(map.get("hiddenId"));
                    if (StrUtil.isNotEmpty(hiddenId)) {
                        hiddenCount ++;
                        hiddenIds.add(Integer.valueOf(hiddenId));
                    }
                }
            }
            HZJCount += Jlist.size();
            HZJWCount += JCount;
            // 获取完成率
            String Jrate = "0.00%";
            if (Jlist.size() != 0) {
                Double b = JCount / Jlist.size() * 100;
                Jrate = NumberUtil.round(b,2) + "%";
            } else {
                Jrate = "100.00%";
            }
            // 获取操作负责人在某个时间段的任务
            List<Map<String,String>> Clist = preventionDeviceMapper.getDutyCount(startTime,endTime,dangerName,"3");
            // 任务完成数
            Double CCount = 0.00;
            for (Map<String, String> map : Clist) {
                String state = map.get("taskState");
                if (state.equals("已排查")) {
                    CCount ++;
                }
                if (map.containsKey("hiddenId")) {
                    String hiddenId = String.valueOf(map.get("hiddenId"));
                    if (StrUtil.isNotEmpty(hiddenId)) {
                        hiddenCount ++;
                        hiddenIds.add(Integer.valueOf(hiddenId));
                    }
                }
            }
            HZCCount += Clist.size();
            HZCWCount += CCount;
            // 获取完成率
            String Crate = "0.00%";
            if (Clist.size() != 0) {
                Crate = NumberUtil.round(CCount / Clist.size() * 100,2) + "%";
            } else {
                Crate = "100.00%";
            }
            // 获取总完成任务数
            List<Map<String,String>> ZWlist = preventionDeviceMapper.getDutyCount(startTime,endTime,dangerName,null);
            // 任务完成数
            Double ZWCount = 0.00;
            for (Map<String, String> map : ZWlist) {
                String state = map.get("taskState");
                if (state.equals("已排查")) {
                    ZWCount ++;
                }
                if (map.containsKey("hiddenId")) {
                    String hiddenId = String.valueOf(map.get("hiddenId"));
                    if (StrUtil.isNotEmpty(hiddenId)) {
                        hiddenCount ++;
                        hiddenIds.add(Integer.valueOf(hiddenId));
                    }
                }
            }
            Double QCount = ZWCount;
            HZWCount += ZWCount;
            HZCount += ZWlist.size();
            // 获取总完成率
            String Qrate = "0.00%";
            if (ZWlist.size() != 0) {
                Qrate = NumberUtil.round(ZWCount / ZWlist.size() * 100,2) + "%";
            } else {
                Qrate = "100.00%";
            }
            HZHiddenCount += hiddenIds.size();
            dutyUserCountVO.setDangerName(dangerName);
            dutyUserCountVO.setQtaskCount(QCount.intValue() + "");
            dutyUserCountVO.setQrate(Qrate);
            dutyUserCountVO.setZtaskCount(ZCount.intValue() + "");
            dutyUserCountVO.setZrate(Zrate);
            dutyUserCountVO.setJtaskCount(JCount.intValue() + "");
            dutyUserCountVO.setJrate(Jrate);
            dutyUserCountVO.setCtaskCount(CCount.intValue() + "");
            dutyUserCountVO.setCrate(Crate);
            dutyUserCountVO.setHiddenCount(hiddenCount.toString());
            if (hiddenIds.size() == 0) {
                dutyUserCountVO.setCompleteHiddenCount("0");
            } else {
                Integer comCount = 0;
                for (Integer hiddenId : hiddenIds) {
                    PreventionHiddenRegister preventionHiddenRegister = preventionHiddenRegisterMapper.queryById(hiddenId);
                    if (preventionHiddenRegister.getHiddenProgress().equals("已完成")) {
                        comCount ++;
                        HZWHiddenCount ++;
                    }
                }
                dutyUserCountVO.setCompleteHiddenCount(comCount.toString());
            }
            dutyUserCountVOS.add(dutyUserCountVO);
        }
        dutyUserCount.setDangerName("huizong");
        dutyUserCount.setQtaskCount(HZWCount.intValue() + "");
        dutyUserCount.setQrate(HZCount == 0.00 ? "100.00%" : NumberUtil.round(HZWCount / HZCount * 100,2) + "%");
        dutyUserCount.setZtaskCount(HZZWCount.intValue() + "");
        dutyUserCount.setZrate(HZZCount == 0.00 ? "100.00%" : NumberUtil.round(HZZWCount / HZZCount * 100,2) + "%");
        dutyUserCount.setJtaskCount(HZJWCount.intValue() + "");
        dutyUserCount.setJrate(HZJCount == 0.00 ? "100.00%" : NumberUtil.round(HZJWCount / HZJCount * 100,2) + "%");
        dutyUserCount.setCtaskCount(HZCWCount.intValue() + "");
        dutyUserCount.setCrate(HZCCount == 0.00 ? "100.00%" : NumberUtil.round(HZCWCount / HZCCount * 100,2) + "%");
        dutyUserCount.setHiddenCount(HZHiddenCount.toString());
        dutyUserCount.setCompleteHiddenCount(HZWHiddenCount.toString());
        dutyUserCountVOS.add(dutyUserCount);
        return dutyUserCountVOS;
    }
}
